package com.service.business.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.business.support.SessionUtil;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.enums.NotifyMessageEnums;
import com.service.dao.mapper.NewsMapper;
import com.service.dao.mapper.NotifyMessageMapper;
import com.service.dao.model.News;
import com.service.business.news.controller.rep.NewsDetailRep;
import com.service.business.news.controller.rep.NewsPageRep;
import com.service.business.news.controller.req.NewsPageReq;
import com.service.business.news.service.NewsService;
import com.service.business.support.Language;
import com.service.dao.model.NotifyMessage;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@SuppressWarnings("ALL")
@Service("newsService")
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private NotifyMessageMapper notifyMessageMapper;

    @Override
    public CommonPageRep<NewsPageRep> pageNews(Integer newsStatus,NewsPageReq newsPageReq) {
        Page<News> page = PageHelper.startPage(newsPageReq.getPageNo(),newsPageReq.getPageSize());
        QueryWrapper<News> wrapper = new QueryWrapper<>();
        wrapper.eq("language", Language.LANG.get());
        wrapper.eq("news_status", newsStatus);
        wrapper.eq("is_new", newsPageReq.getIsNew());
        baseMapper.selectList(wrapper.orderByDesc("id"));
        List<NewsPageRep> newsPageRepList = page.getResult().stream().map(item->{
            NewsPageRep newsPageRep = ModelMapperUtil.getStrictModelMapper().map(item, NewsPageRep.class);
            return newsPageRep;
        }).collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, newsPageRepList);
    }

    @Override
    @Transactional
    public NewsDetailRep getOneNewsDetail(Integer newsId) {
        News news = baseMapper.selectById(newsId);
        news.setNewsVisit(news.getNewsVisit()+1);
        baseMapper.updateById(news);
        //用户查阅新闻详情，设置当前用户的该新闻消息已读
        Integer memberId = sessionUtil.getUser().getUserId();

        NotifyMessage notifyMessage = new NotifyMessage();
        notifyMessage.setMessageType(NotifyMessageEnums.NEW_TYPE.getCode());
        notifyMessage.setMemberId(memberId);
        notifyMessage.setTargetId(news.getId());
        notifyMessage.setCreateTime(new Date());
        notifyMessage.setIsRead(NotifyMessageEnums.IS_READ.getCode());
        notifyMessageMapper.insert(notifyMessage);

        return ModelMapperUtil.getStrictModelMapper().map(news,NewsDetailRep.class);
    }

    @Override
    public List<News> listAllNews() {
        return list(new LambdaQueryWrapper<News>()
                .eq(News::getLanguage, Language.LANG.get())
                .eq(News::getNewsStatus, CommonStatusEnums.ACTIVE.getCode()));
    }
}
